package cn.tedu.tedutest02202.service.impl;

import cn.tedu.tedutest02202.ex.ServiceException;
import cn.tedu.tedutest02202.mapper.EmailMapper;
import cn.tedu.tedutest02202.pojo.dto.EmailDTO;
import cn.tedu.tedutest02202.pojo.dto.EmailLoginDTO;
import cn.tedu.tedutest02202.pojo.dto.EmailUpdateDTO;
import cn.tedu.tedutest02202.pojo.dto.UserDTO;
import cn.tedu.tedutest02202.pojo.entity.Email;
import cn.tedu.tedutest02202.pojo.vo.EmailLoginVO;
import cn.tedu.tedutest02202.pojo.vo.EmailVO;
import cn.tedu.tedutest02202.service.EmailService;
import cn.tedu.tedutest02202.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.List;

@Service
@Slf4j
public class IEmailService implements EmailService {

    @Autowired
    EmailMapper mapper;

    @Override
    public void insertNewEmail( EmailDTO email) {
        log.debug("用户提交的信息：{}",email);
        String username = email.getUsername();
        String emailString = email.getEmail();
        String password = email.getPassword();
        int emailCount = mapper.countByEmail(emailString);
        int usernameCount = mapper.countByUsername(username);
        if(emailCount>0){
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,"邮箱已被占用，请更换邮箱号！");
        }
        if(usernameCount>0){
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,"用户名已被占用，请更换用户名！");
        }
        Email emailEntity = new Email();
        BeanUtils.copyProperties(email,emailEntity);
        log.debug("规则校验通过，即将写入数据表！");
        mapper.addNew(emailEntity);
    }

    @Override
    public void deleteById(Long id) {
        log.debug("用户提交的id：{}",id);
        EmailVO email = mapper.selectById(id);
        log.debug("查找到的数据是：{}",email);
        if(email==null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"删除的数据不存在！");
        }
        mapper.deleteById(id);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        log.debug("开始执行【根据ID批量删除数据】的业务，参数:{}",ids);
        if(ids.size()==0){

        }
        int i = mapper.deleteIds(ids);
        log.debug("成功删除{}条数据",i);
    }

    @Override
    public void updateById(Long id, EmailUpdateDTO emailUpdateDTO) {
        log.debug("开始执行【根据id修改数据】的业务，id:{}，修改的数据：{}",id,emailUpdateDTO);
        //先根据id，查询数据，判断能否查到这条数据
        EmailVO emailVO = mapper.selectById(id);
        if(emailVO==null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"该数据不存在！");
        }
        //如果存在这条数据，则去判断需要修改的内容是否和其他数据有所冲突，用户名唯一、邮箱唯一
        //因此需要：查询除此id之外符合输入的用户名、或符合输入的邮箱的数据，如果存在，则不允许修改
        int i = mapper.countByNameAndNotId(id, emailUpdateDTO.getUsername());
        if(i>0){
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,"用户名已被占用！");
        }
        int i1 = mapper.countByEmailAndNotId(id, emailUpdateDTO.getEmail());
        if(i1>0){
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,"邮箱已被占用！");
        }
        Email email = new Email();
        BeanUtils.copyProperties(emailUpdateDTO,email);
        email.setId(id);
        mapper.updateById(email);
    }


    @Override
    public EmailVO selectStandardEmailById(Long id) {
        log.debug("开始执行【根据id查询Email数据】的业务，参数:{}",id);
        EmailVO email = mapper.selectById(id);
        if(email==null){
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,"查找的的数据不存在！");
        }
        return email;
    }

    @Override
    public List<EmailVO> selectAll() {
        log.debug("开始执行【查询数据列表的业务】");
        List<EmailVO> emailVOS = mapper.selectAll();
        return emailVOS;
    }

    @Override
    public Integer countAll() {
        log.debug("开始执行【统计数据总量】的业务");
        int i = mapper.countAll();
        return i;
    }

    @Override
    public void login(EmailLoginDTO emailLoginDTO) {
        log.debug("用户输入的登录账号和密码是：{}",emailLoginDTO);
        String loginEmail = emailLoginDTO.getEmail();
        String loginPassword = emailLoginDTO.getPassword();
        EmailLoginVO emailLoginVO = mapper.selectPasswordByEmail(loginEmail);
        log.debug("查询到的数据是：{}",emailLoginVO);
        if(emailLoginVO==null){
            String message = "账号不存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        String password = emailLoginVO.getPassword();
        if(!password.equals(loginPassword)){
            String message= "密码错误！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERROR_WRONG_PASSWORD,message);
        }
        log.debug("密码正确，登录成功！");
    }


}
